<style lang="scss" scoped>
</style>
<template>
  <page-box>
    <div class="ui-search-box" slot="search-form">
      <el-form class="ui-search-form" label-width="120px" :model="params" ref="form">
        <el-form-item label="报修单号" prop="repairId">
          <el-input size="mini" class="ui-item-reset" v-model="params.repairId" clearable></el-input>
        </el-form-item>
        <el-form-item label="报修种类" prop="repairKind">
          <el-select placeholder="请选择" size="mini" class="ui-item-reset" v-model="params.repairKind" clearable>
            <el-option label="物资损坏" value="1"></el-option>
            <el-option label="网络故障" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报修原因" prop="repairReason">
          <el-input size="mini" class="ui-item-reset" v-model="params.repairReason" clearable></el-input>
        </el-form-item>
        <el-form-item label="每页数据">
          <el-select size="mini" class="ui-item-reset"  v-model="params.pageSize">
            <el-option label="5" :value="5"></el-option>
            <el-option label="10" :value="10"></el-option>
            <el-option label="20" :value="20"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="ui-search-btus">
        <el-button type="primary" size="mini" @click="getData">搜索</el-button>
        <el-button  size="mini" @click="resetForm('form')">重置</el-button>

      </div>
    </div>
    <div class="ui-list ui-box" slot="list-box">
      <div class="box-header ui-list-header">
        <div>报修列表</div>
        <div>
          <el-button type="primary" size="mini" @click="exportRepairExcel">导出</el-button>
          <el-button type="primary" size="mini" @click="open">新增</el-button>
        </div>
      </div>
      <el-table :data="dataList" @selection-change="handleSelectionChange" :cell-style="cellStyle"
                :header-cell-style="rowClass" v-loading="tabLoading">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="repairId" label="报修单号"></el-table-column>
        <el-table-column prop="repairKind" label="报修种类">
          <template v-if="scope.row.repairKind!=null" slot-scope="scope">
            <span v-if="scope.row.repairKind===1">物资损坏</span>
            <span v-else>网络故障</span>
          </template>
        </el-table-column>
        <el-table-column prop="repairReason" label="报修原因"></el-table-column>
        <el-table-column prop="submitDate" label="提交日期"></el-table-column>
        <el-table-column prop="resolvedDate" label="解决日期"></el-table-column>
        <el-table-column prop="buildingNo" label="宿舍楼"></el-table-column>
        <el-table-column prop="dorNo" label="宿舍号"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-button type="success" icon="el-icon-check" circle size="mini" v-if="scope.row.status===1"></el-button>
            <el-button type="danger" icon="el-icon-close" circle v-else size="mini"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="checkDetail(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" @click="deleteItem(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="ui-pagination"
        @current-change="changPageNum"
        layout="total, prev, pager, next"
        :current-page="this.params.pageNum"
        :page-size="this.params.pageSize"
        :total="total">
      </el-pagination>
      <add-item v-bind:repairItem.sync='repairItem' @submit="addRepair" ref="addItem"></add-item>
    </div>
  </page-box>
</template>
<script>
  import pageBox from '@/components/pageBox'
  import addItem from '@/components/repair/addItem.vue'

  export default {
    name: 'repairList',
    components: {
      pageBox, addItem
    },
    data() {
      return {
        dataList: [],
        exportRepairList: [],
        params: {
          repairId:'',
          repairKind: '',
          repairReason: '',
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        tabLoading: false,
        repairItem:{
          repairId:'',
          repairKind: '',
          repairReason: '',
          buildingNo:'',
          dorNo:''
        }

      }
    },
    created() {
      this.getData()
    },
    methods: {
      open() {
        this.$refs.addItem.openWindow('申请报修')
      },
      addRepair() {
        let dorId = this.repairItem.dorNo
        post('/pms_api/repair/add', {
          'dorId': dorId,
          'repairKind': this.repairItem.repairKind,
          'repairReason': this.repairItem.repairReason
        }).then(res => {
          this.$refs.addItem.addDialogVisible = false;
          this.getData();
          this.tabLoading = false;
        }).catch(err => {
          this.tabLoading = false
          console.log(err)
        })
      },
      changPageNum(val) {
        this.params.pageNum = val
        this.getData()
      },
      getData() {
        post('/pms_api/repair/list', this.params).then(res => {
          this.dataList = res.data
          this.total = res.totalPages
          console.log(res.data)
          this.tabLoading = false
        }).catch(err => {
          this.tabLoading = false
          console.log(err)
        })
      },
      checkDetail(item) {
        this.$router.push({
          path: '/repairDetail',
          query: {
            repairId: item.repairId
          }
        })
      },
      deleteItem(item) {
        post('/pms_api/repair/delete', {'repairId': item.repairId}).then(res => {
          this.getData()
        }).catch(err => {
          console.log(err)
          this.tabLoading = false
        })
      },
      handleSelectionChange(val) {
        this.exportRepairList = val
      },
      exportRepairExcel() {
        let repairKind = {1: '物资损坏', 2: '网络故障'};
        if (this.exportRepairList.length > 0) {
            this.exportRepairList.forEach(item => {
              item.repairKind = repairKind[item.repairKind]
            });
          const tHeader = ['报修单号', '报修种类', '报修原因', '提交日期', '解决日期', '宿舍楼', '宿舍号','状态'];
          const filterVal = ['repairId', 'repairKind', 'repairReason', 'submitDate', 'resolvedDate', 'buildingNo', 'dorNo', 'status'];
          const list = this.exportRepairList;
          this.$getGlobal.exportFun('报修详情', tHeader, filterVal, list);
        } else {
          this.$message.error('请先选择需要导出的列表项');
        }
      },
      //表格居中
      cellStyle({row, colum, rowIndex, columIndex}) {
        return "text-align: center";
      },
      rowClass({row, rowIndex}) {
        return "text-align: center";
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.getData()
      },
    }
  }
</script>


